package com.qingcloud.adminbackend.controller;

import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.entity.source.DataSource;
import com.qingcloud.adminbackend.service.IDataSourceService;
import com.qingcloud.adminbackend.service.IDatasourceDatabaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author qym
 * @Description 数据源管理api
 * @Date 下午4:33 2021/3/29
 **/
@RestController
@RequestMapping("/datasource")
public class TdDatasourceController {

    @Resource
    private IDataSourceService dataSourceService;

    @Resource
    IDatasourceDatabaseService datasourceDatabaseService;

    @ApiOperation(value = "新增数据源", notes = "新增数据源")
    @PostMapping("/add")
    public AjaxResult addDatasource(@RequestBody DataSource dataSource) {
        return dataSourceService.addDatasource(dataSource);
    }

    @ApiOperation(value = "删除数据源", notes = "删除数据源")
    @GetMapping("/deleteByIds")
    public AjaxResult deleteDatasourceByIds(@RequestParam("ids") String ids) {
        return dataSourceService.deleteDatasourceByIds(ids);
    }

    @ApiOperation(value = "修改数据源", notes = "修改数据源")
    @PostMapping("/update")
    public AjaxResult updateDatasource(@RequestBody DataSource dataSource) {
        return dataSourceService.updateDatasource(dataSource);
    }

    @ApiOperation(value = "查询数据源指定id", notes = "查询数据源指定id", response = DataSource.class)
    @GetMapping("/findByIds")
    public AjaxResult<List<DataSource>> findDatasourceByIds(@RequestParam("ids") String ids) {
        return dataSourceService.findDatasourceByIds(ids);
    }

    @ApiOperation(value = "复制数据源指定id", notes = "复制数据源指定id", response = DataSource.class)
    @GetMapping("/copyById")
    public AjaxResult copyDatasourceById(@RequestParam("id") String id) {
        return dataSourceService.copyDatasourceById(id);
    }

    @ApiOperation(value = "查询数据源指定订单id", notes = "查询数据源指定订单id", response = DataSource.class)
    @GetMapping("/findByProjectId")
    public AjaxResult<List<DataSource>> findDatasourceByProjectId(@RequestParam("projectId") String projectId) {
        return dataSourceService.findDatasourceByProjectId(projectId);
    }

    @ApiOperation(value = "模糊查询数据源指定name或者projectId", notes = "模糊查询数据源指定name或者projectId", response = DataSource.class)
    @GetMapping("/findByName")
    public AjaxResult<List<DataSource>> findDatasourceByName(@RequestParam(value = "name", required = false) String name,
                                                             @RequestParam(value = "projectId", required = false) String projectId) {
        return dataSourceService.findDatasourceByName(name, projectId);
    }

    @ApiOperation(value = "获取数据源所有类型", notes = "获取数据源所有类型", response = String.class)
    @GetMapping("/getTypes")
    public AjaxResult getTypes() {
        return dataSourceService.getTypes();
    }

    @ApiOperation(value = "测试数据源", notes = "测试数据源")
    @PostMapping("/test/{dsId}")
    public AjaxResult test(@PathVariable("dsId") Integer dsId) {
        return datasourceDatabaseService.test(dsId);
    }

}
